package com.aiyige.page.mediaslider.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.aiyige.page.mediaslider.PhotoSliderItemPage;
import com.aiyige.page.mediaslider.SelectablePage;
import com.aiyige.page.mediaslider.VideoSliderItemPage;
import com.aiyige.page.mediaslider.model.Media;
import com.aiyige.utils.ListUtil;

import java.util.List;

public class MediaSliderAdapter extends FragmentStatePagerAdapter {
    List<Media> mediaList;
    List<SelectablePage> selectablePageList;

    public MediaSliderAdapter(FragmentManager fm, List<Media> mediaList) {
        super(fm);
        this.mediaList = mediaList;
        for (Media media : mediaList) {
            switch (media.getType()) {
                case Media.MEDIA_TYPE_PHOTO: {
                    PhotoSliderItemPage photoSliderItemPage = PhotoSliderItemPage.newInstance(media);
                    selectablePageList.add(photoSliderItemPage);
                    break;
                }
                case Media.MEDIA_TYPE_VIDEO: {
                    VideoSliderItemPage videoSliderItemPage = VideoSliderItemPage.newInstance(media);
                    selectablePageList.add(videoSliderItemPage);
                    break;
                }
            }
        }
        if (!ListUtil.isEmpty(mediaList)) {
            selectablePageList.get(0).onSelectChanged(true);
        }
    }

    @Override
    public Fragment getItem(int position) {
        return (Fragment) selectablePageList.get(position);
    }

    @Override
    public int getCount() {
        return selectablePageList.size();
    }

    public void currentSelectedIndex(int index) {
        for (int i = 0; i < getCount(); ++i) {
            selectablePageList.get(i).onSelectChanged(i == index);
        }
    }
}
